package com.ruoyi.common.utils.qrcode;

import cn.hutool.core.util.IdUtil;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class MyTest {
//    public static void main(String[] args) {
//        System.out.println("开始生成...");
//        code();
//        System.out.println("生成完毕！");
//    }
    public static void code() {
        try {
            String content = "https://www.baidu.com";
            String path = "F:/测试";// 二维码保存的路径
            String codeName = IdUtil.fastSimpleUUID().toString();// 二维码的图片名
            String imageType = "jpg";// 图片类型
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, 400, 400, hints);
            File file1 = new File(path, codeName + "." + imageType);
            File file = new File(path);
            // 当文件夹不存在时，mkdirs会自动创建多层目录，区别于mkdir．(mkdir如果父目录不存在则会抛出异常)
            if (!file.exists() && !file.isDirectory()) {
                file.mkdirs();
            }
            //MatrixToImageWriter.writeToFile(bitMatrix, imageType, file1);
            //String imgPath = "D:/Desktop/logo.png";
            String fileName = IdUtil.fastSimpleUUID();
            String imagePath = "https://wsdz-dev.obs.cn-north-4.myhuaweicloud.com:443/image%2Fe1a90a820a46497db66cba34691c630b.png";
            QRCodeUtil.encode(content,imagePath,path,true);
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
